
-----兼容mysql函数
drop function date_format(timestamp,text);
CREATE FUNCTION date_format(timestamp, text) RETURNS text AS $$
BEGIN
RETURN to_char($1, $2);
END;
$$ LANGUAGE plpgsql;

drop function date_format(text,text);
CREATE FUNCTION date_format(text, text) RETURNS text AS $$
BEGIN
RETURN to_char(to_timestamp($1,'YYYY-MM-DD HH24:MI:SS'), $2);
END;
$$ LANGUAGE plpgsql;

drop function find_in_set(text,text);
CREATE OR REPLACE FUNCTION find_in_set(text, text) RETURNS boolean AS $$
DECLARE
elements TEXT[];
    element TEXT;
position INTEGER := 0;
BEGIN
    elements := string_to_array($2, ',');
    FOREACH element IN ARRAY elements
    LOOP
        IF $1 = element THEN
            RETURN true;
END IF;
position := position + 1;
END LOOP;
RETURN false; -- Not found
END;
$$ LANGUAGE plpgsql IMMUTABLE;

drop function find_in_set(bigint,text);
CREATE OR REPLACE FUNCTION find_in_set(bigint, text) RETURNS boolean AS $$
DECLARE
elements TEXT[];
    element TEXT;
position INTEGER := 0;
BEGIN
    elements := string_to_array($2, ',');
    FOREACH element IN ARRAY elements
    LOOP
        IF CAST($1 AS VARCHAR) = element THEN
            RETURN true;
END IF;
position := position + 1;
END LOOP;
RETURN false; -- Not found
END;
$$ LANGUAGE plpgsql IMMUTABLE;


drop function isnull(text,text);
CREATE OR REPLACE FUNCTION isnull(text, text) RETURNS text AS $$
DECLARE
element TEXT;
BEGIN

RETURN COALESCE($1,$2); -- Not found
END;
$$ LANGUAGE plpgsql IMMUTABLE;

drop function isnull(bigint,text);
CREATE OR REPLACE FUNCTION isnull(bigint, text) RETURNS text AS $$
DECLARE
element TEXT;
BEGIN

RETURN COALESCE(CAST($1 AS VARCHAR),$2); -- Not found
END;
$$ LANGUAGE plpgsql IMMUTABLE;



drop function sysdate();
CREATE OR REPLACE FUNCTION sysdate() RETURNS TIMESTAMP AS $$
BEGIN

RETURN now(); -- Not found
END;
$$ LANGUAGE plpgsql IMMUTABLE;